/**
 * 追号记录部分前端代码
 */

var item_index = 0;
var page_limit = parseInt($('#id_page_limit').val());
// 获取页面数据
var resData = '';
// 发出的请求数据参数
var param      = 'operation=money_log';
var appointUid = $('#appointUid').val();
var isagent    = $('#isagent').val();
if(!$.isEmptyObject(appointUid)){
	param     += '&appoint_uid=' + appointUid;
}
if(!$.isEmptyObject(isagent)){
	param     += '&isagent=' + isagent;
}
$.ajax({  
	type:'post',    
    url: getDataUrl,  
    data:param,  
    cache:false,  
    dataType:'json',  
    async:false,
    success:function(res){  
    	resData = res;
    }  
})

console.log(resData);
$('#id_page_box').on('click', '.tag_page_item', function (){
	$('.tag_page_item').removeClass('active');
	$(this).addClass('active');
	setBetRecordList();
})

$('#tag_search_time a').on('click', '', function (){
	if($(this).attr('class') == 'userSearch time active') return;
	$('#tag_search_time a').removeClass('active');
	$(this).addClass('active');
	$('.tag_page_item').removeClass('active');
	setBetRecordList();
})

$('#tag_search_status a').on('click', '', function (){
	if($(this).attr('class') == 'userSearch time active') return;
	$('#tag_search_status a').removeClass('active');
	$(this).addClass('active');
	$('.tag_page_item').removeClass('active');
	setBetRecordList();
})

$('#id_log_item').on('change', '', function (){
	setBetRecordList();
})

setBetRecordList();
//createPage();

function setBetRecordList(np=''){
	if($.isEmptyObject(resData)) return;
	$('#id_all_rows').html('0');
	if(!np){
		np = $('#id_page_box li[class="tag_page_item active"]').attr('ami');
	}
	if(np == -1){
		var basen = parseInt(resData.moneyLog.length)/parseInt(page_limit);
		var duoyi = parseInt(resData.moneyLog.length)%parseInt(page_limit);
		np = (duoyi > 0) ? (basen + 1) : (basen);
	}
	np     = np ? parseInt(np) : 1;
	// 获取筛选信息
	var limit_day   = '';
	var qujian_day  = '';
	var day_case    = $('#tag_search_time a[class="userSearch time active"]').index().toString();
	
	switch(day_case){
		case '0':
			limit_day = resData.pageSuport.today;
			break;
		case '1':
			limit_day   = resData.pageSuport.yesterday;
			qujian_day  = resData.pageSuport.today;
			break;
		case '2':
			limit_day = resData.pageSuport.week;
			break;
		default:
			break;
	}
	var log_type  = '';
	var type_case = $('#tag_search_status a[class="userSearch time active"]').index().toString();
	switch(type_case){
		case '0':
			log_type = '';
			break;
		case '1':
			log_type = 'recharge';
			break;
		case '2':
			log_type = 'postal';
			break;
		default:
			break;
	}
	var item_type = $('#id_lot_item').val();
	var get_day   = $('#tag_search_time a[class="userSearch time active"]').index();
	
	var log_selection = $('#id_log_item').val();
	
	// 初始化列表
	$('#id_moneyLog_list').html('');
	var c_time = '';
	item_index = 0;
	var b_item_index = parseInt(page_limit) * ( np - 1);
	var e_item_index = 0;
	b_item_index = b_item_index ? b_item_index : 0;
	$.each(resData.moneyLog, function (i, v){
		c_time = parseInt(v.dateline);
		// 时间筛选
		if(qujian_day){
			if(c_time > qujian_day || c_time < limit_day) return true;
		}else{
			if(c_time < limit_day) return true;
		}
		// 类型筛选
		if(log_type != '' && v.tid != log_type) return true;
		// 类别筛选
		if(log_selection != '' && log_selection != v.tid) return true;
		if(item_index > (b_item_index + page_limit) || 
				item_index < b_item_index) {
			item_index++;
			return true;
		}
		zhaiyao = '';
		remark  = '';
		income  = '---';
		outcome = '---';
		switch(v.tid){
			case 'order_add':
				zhaiyao = '投注扣款';
			case 'postal':
				zhaiyao = zhaiyao ? zhaiyao : '提现';
				outcome = v.money;
				break;
			case 'recharge':
				zhaiyao = '充值';
			case 'winner':
				zhaiyao = zhaiyao ? zhaiyao : '奖金派送';
				income  = v.money;
				break;
			case 'betting_award':
				zhaiyao = '奖金派送';
				income  = v.money;
				break;
			case 'repoint':
				zhaiyao = '代理返点';
				income  = v.money;
				break;
			default:
				break;
		}
		tolong = 'display: none;';
		var tr  = '<tr style="font-size: 12px;">';
		tr      += '	<td style="color: rgb(74, 169, 219);">' + v.order_no + '</td>';
		tr      += '	<td>' + v.date_dateline + '</td>';
		tr      += '	<td>' + zhaiyao + '</td>';
		tr      += '	<td>' + income + '</td>';	
		tr      += '	<td>' + outcome + '</td>';
		tr      += '	<td>' + v.balance + '</td>';
		tr      += '	<td><em>' + v.notes + '</em></td>';
		tr 		+= '</tr>';
		$('#id_moneyLog_list').prepend(tr);
		item_index++;
	})
	createPage(np, item_index);
}

$('#id_page_box').on('click', '.tag_first_page', function (){
	setBetRecordList(1);
})
$('#id_page_box').on('click', '.tag_up_page', function (){
	var np       = 1;
	var now_page = parseInt($('#id_page_box li[class="tag_page_item active"]').attr('ami'));
	if(now_page > 1) np = now_page -1;
	setBetRecordList(np);
})
$('#id_page_box').on('click', '.tag_down_page', function (){
	var np       = 1;
	var now_page = parseInt($('#id_page_box li[class="tag_page_item active"]').attr('ami'));
	np = now_page + 1;
	setBetRecordList(np);
})
$('#id_page_box').on('click', '.tag_end_page', function (){
	setBetRecordList(-1);
})

/**
 * np: 当前页面
 */
function createPage(np='', allNums=''){
	if(!np){
		np       = $('#id_page_box li[class="tag_page_item active"]').attr('ami');
	}
	np           = np ? parseInt(np) : 1;
	var pageNums = parseInt(allNums/page_limit);
	if((parseInt(allNums)%page_limit) > 0) {
		pageNums++;
	}
	// 如果小于1， 则不需要分页数据
	if(pageNums < 1) {
		$('#id_page_box').html('');
		return;
	}
	var pageHtml      = '';
	var firstPageHtml = '<li class="tag_first_page"><a href="javascript:void(0);">第一页</a></li>';
	var upPageHtml    = '<li class="tag_up_page"><a href="javascript:void(0);">上一页</a></li>';
	var downPageHtml  = '<li class="tag_down_page"><a href="javascript:void(0);">下一页</a></li>';
	var endPageHtml   = '<li class="tag_end_page"><a href="javascript:void(0);">最后一页</a></li>';
	var pageChoice    = '';
	var i     = 1;
	times     = 5;
	if(pageNums < times) times = pageNums;
	if(np > 3){
		// 标签标识往后移
		i = np - 2;
		// 检查是否到最后一页了
		if((np + 2) < pageNums){
			times = np + 2;
		}else{
			times = pageNums;
		}
	}
	if(!allNums) return;
	for(i; i <= times; i++){
		pageChoice   = '';
		if(np == i) pageChoice = 'active';
		pageHtml += '<li ami="' + i + '" class="tag_page_item ' + pageChoice + '"><a href="javascript:void(0);">' + i + '</a></li>';
	}
	$('#id_all_rows').html(allNums);
	// 当前循环没有数据，不做分页构造
	$('#id_page_box').html(firstPageHtml + upPageHtml + pageHtml + downPageHtml + endPageHtml);
	$('#id_page_box').show();
}









